package com.example.projectsalegoods;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import android.os.Bundle;
import android.provider.Contacts;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.Editable;

public class MainSaleActivity extends Activity implements OnClickListener {
	private DataBaseHelper mHender;
	private TextView mTextViewName, mTextViewPass;
	private EditText mName, mPass;
	private Button mLogin;
	private SQLiteDatabase myDataBase;
	public String user[] = new String[4];
	public String pass[] = new String[4];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_sale);
		mHender = new DataBaseHelper(this);
		mTextViewName = (TextView) findViewById(R.id.txt_Name);
		mTextViewPass = (TextView) findViewById(R.id.txt_Pass);
		mName = (EditText) findViewById(R.id.e_Name);
		mPass = (EditText) findViewById(R.id.e_Pass);
		mLogin = (Button) findViewById(R.id.btnlogin);
		mLogin.setOnClickListener(this);
		try {
			mHender.createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// mHender.openDataBase();
		try {
			mHender.copyDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		myDataBase = mHender.getWritableDatabase();
		Cursor mCursor = myDataBase.rawQuery("SELECT UserName FROM USER", null);
		Cursor mPassCursor = myDataBase.rawQuery("SELECT PassWord FROM USER",
				null);
		if (mCursor != null) {
			if (mCursor.moveToFirst()) {
				for (int x = 0; x < 2; x++) {
					user[x] = (mCursor.getString(mCursor
							.getColumnIndex("UserName")));
					mCursor.moveToNext();
				}
			}

		}
		if (mPassCursor != null) {
			if (mPassCursor.moveToFirst()) {
				for (int j = 0; j < 2; j++) {
					pass[j] = (mPassCursor.getString(mPassCursor
							.getColumnIndex("PassWord")));
					mPassCursor.moveToNext();
				}
			}

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main_sale, menu);
		return true;
	}

	public void onClick(View v) {
		String name = mName.getText().toString().trim();
		String mpass = mPass.getText().toString().trim();
		// int count = gur.length;
		/*for (int j = 0; j < 2; j++) {
			if (user[j].toString().trim().equals(name)) {
				Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
				for (int i = 0; i < 2; i++) {
					if (pass[i].toString().trim().equals(mpass)) {
						Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
						// Intent mIntent = new Intent(this,inputValue.class);
					} else {
						Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
					}
				}
			}
			else{
				Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
			}
		}*/
		/**
		 * Test 2 equals name.
		 * * doan nay can xem lai. khi no duyet qua mot mang. gap gia tri dung vao sai !!
		 * doan nay can hoi thay
		 */
		for (int j = 0; j < 2; j++) {
			if(name.equalsIgnoreCase(user[j].toString().trim()))
			{
			for (int i = 0; i < 2; i++) {
				if(mpass.equalsIgnoreCase(pass[i].toString().trim())){
					
					Intent mIntent = new Intent(this,mainUserInterface.class);
					startActivity(mIntent);
				} else {
					Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
				}
			}
			/*if (user[j].toString().trim().equals(name) && (pass[i].toString().trim().equals(mpass))) 
			(name.equalsIgnoreCase(user[j].toString().trim()) && mpass.equalsIgnoreCase(pass[i].toString().trim()))
			{
					} */
			
				}
		
			}
	
		}
	

	/*
	 * public void checkName() { String name =
	 * mName.getText().toString().trim(); // int count = gur.length; for (int j
	 * = 0; j < 2; j++) { if ( gur[j].toString().trim().equals(name)
	 * gur[j].toString().contains(name)) { Toast.makeText(this, "Pass",
	 * Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Fail",
	 * Toast.LENGTH_LONG).show(); } // Toast.makeText(this, "Name :" + gur[j],
	 * // Toast.LENGTH_LONG).show(); } }
	 */
}
